//

// (  )
//

package com.tools.acceleratecore.tool.impl;

import android.content.Context;
import com.tools.acceleratecore.AccelerateCoreFactory;
import com.tools.acceleratecore.tool.intf.INativeMgr;
import com.tools.acceleratecore.tool.intf.INativeMgrListener;
import com.tools.acceleratecore.xlib.XLibFactory;
import com.tools.acceleratecore.xlib.xlib.impl.XObserver;
import com.tools.acceleratecore.xlib.xlib.intf.IXThreadTask;
import com.tools.acceleratecore.xlib.xlib.intf.IXThreadTaskListener;
import java.util.Iterator;
import java.util.List;

public class NativeMgr extends XObserver<INativeMgrListener> implements INativeMgr {
    private Context mContext = null;
    private boolean mInited = false;
    private IXThreadTask mIXThreadTaskInit = null;
    private IXThreadTask mIXThreadTaskInvoke = null;

    public native void nativeInvoke(String var1);

    public NativeMgr() {
        this.mContext = AccelerateCoreFactory.getApplication();
        this._init();
    }

    private void _init() {
        this.mIXThreadTaskInit = (IXThreadTask)XLibFactory.getInstance().createInstance(IXThreadTask.class);
        this.mIXThreadTaskInvoke = (IXThreadTask)XLibFactory.getInstance().createInstance(IXThreadTask.class);
    }

    public boolean init() {
        if (this.mInited) {
            return false;
        } else {
            this.mInited = true;
            this.mIXThreadTaskInit.start(new IXThreadTaskListener() {
                public void onThreadWork() {
                }

                public void onThreadComplete() {
                    synchronized(NativeMgr.this.mListListener) {
                        Iterator var3 = NativeMgr.this.getListListener().iterator();

                        while(var3.hasNext()) {
                            INativeMgrListener listener = (INativeMgrListener)var3.next();
                            listener.onNativeMgrInitComplete();
                        }

                    }
                }
            });
            return true;
        }
    }

    public void invokeAsyn(final List<String> listArgv) {
        this.mIXThreadTaskInvoke.start(new IXThreadTaskListener() {
            public void onThreadWork() {
                NativeMgr.this.invokeSyn(listArgv);
            }

            public void onThreadComplete() {
                synchronized(NativeMgr.this.mListListener) {
                    Iterator var3 = NativeMgr.this.getListListener().iterator();

                    while(var3.hasNext()) {
                        INativeMgrListener listener = (INativeMgrListener)var3.next();
                        listener.onInvokeAsynComplete();
                    }

                }
            }
        });
    }

    public void invokeSyn(List<String> listArgv) {
        this.invoke(listArgv);
    }

    private void invoke(List<String> listArgv) {
        if (listArgv != null && listArgv.size() != 0) {
            StringBuffer sbArgv = new StringBuffer();

            for(int nIndex = 0; nIndex < listArgv.size(); ++nIndex) {
                sbArgv.append((String)listArgv.get(nIndex));
                if (nIndex < listArgv.size() - 1) {
                    sbArgv.append(" ");
                }
            }

            this.nativeInvoke(sbArgv.toString());
        }
    }
}
